﻿using software.Common.MVVM;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Software.Common.Util.AppConfig;
using System.IO;
using project.B12.Model.Models;
using project.B12.Model.Enums;
using project.B12.Service;
using project.B12.IService;
using System.Text;
using System.Collections.Generic;

namespace project.B12.ViewModels
{
    public class PublicResourceViewModel : ObservableObject
    {
        #region Model
        private IMICPlateMes mICPlateMes;
        public string LogoImage { get; private set; }//软件logo
        public string SoftwareName { get; private set; }//软件名称

        public string UserName { get; set; }

        public LanguageEnum Language { get; set; }

        private WindowState _windowState = WindowState.Normal;//窗口状态
        public WindowState windowState
        {
            get { return _windowState; }
            set { _windowState = value; this.RaisePropertyChanged(); }
        }

        private ObservableCollection<string> _medicineNames { get; set; }
        public ObservableCollection<string> MedicineNames
        {
            get { return this._medicineNames; }
            set
            {
                _medicineNames = value;
                this.RaisePropertyChanged();
            }
        }

        private ObservableCollection<Table_medicine> _medicines { get; set; }
        public ObservableCollection<Table_medicine> Medicines
        {
            get { return _medicines; }
            set
            {
                _medicines = value;
                SetProjectMedicineNames();
                this.RaisePropertyChanged();
            }
        }

        private ObservableCollection<string> _projectMedicineNames { get; set; }
        public ObservableCollection<string> ProjectMedicineNames
        {
            get { return this._projectMedicineNames; }
            set
            {
                _projectMedicineNames = value;
                this.RaisePropertyChanged();
            }
        }

        private ObservableCollection<string> _bacteriaNames { get; set; }
        public ObservableCollection<string> BacteriaNames
        {
            get { return this._bacteriaNames; }
            set
            {
                _bacteriaNames = value;
                this.RaisePropertyChanged();
            }
        }

        private string upperTemperature {  get; set; }
        public string UpperTemperature
        {
            get { return upperTemperature; }
            set
            {
                upperTemperature = value;
                this.RaisePropertyChanged();
            }
        }

        private string lowerTemperature { get; set; }
        public string LowerTemperature
        {
            get { return lowerTemperature; }
            set
            {
                lowerTemperature = value;
                this.RaisePropertyChanged();
            }
        }

        private List<PlateItem> plateItems { get; set; }
        public List<PlateItem> PlateItems
        {
            get { return plateItems; }
            set
            {
                plateItems = value;
                this.RaisePropertyChanged();
            }
        }

        public Dictionary<string, List<string>> ReportTitleRows { get; set; }

        public int PhotoTime {  get; set; }

        /// <summary>
        /// 判读规则1的菌
        /// </summary>
        public List<string> RecordBacterias { get; set; }
        #endregion

        public PublicResourceViewModel(IMICPlateMes _mICPlateMes)
        {
            RefreshTimeValue();//初始化时间
            mICPlateMes = _mICPlateMes;
            //initialization();//初始化数据源
            double temp = 0;
            // 获取当前运行目录
            string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
            if (UtilAppConfig.ExistItem("LogoImage"))
                LogoImage = Path.Combine(appDirectory, UtilAppConfig.GetAppConfig("LogoImage"));
            if (UtilAppConfig.ExistItem("SoftwareName"))
                SoftwareName = UtilAppConfig.GetAppConfig("SoftwareName");
            if (UtilAppConfig.ExistItem("Temperature"))
                temp =double.Parse(UtilAppConfig.GetAppConfig("Temperature"));
            if (UtilAppConfig.ExistItem("PhotoInterval"))
                PhotoTime = int.Parse(UtilAppConfig.GetAppConfig("PhotoInterval"));
            UpperTemperature =(temp + 0.5).ToString();
            LowerTemperature =(temp - 0.5).ToString();

            _medicineNames = new ObservableCollection<string>();
            _medicines = new ObservableCollection<Table_medicine>();
            _projectMedicineNames =new ObservableCollection<string>();
            PlateItems=new List<PlateItem>();
            ReportTitleRows=new Dictionary<string, List<string>>();
            RecordBacterias = new List<string>();
        }

        private void SetProjectMedicineNames()
        {
            ObservableCollection<string> _projectNames=new ObservableCollection<string>();
            foreach (var item in _medicines)
            {
                string str=item.Abbreviation+" "+ item.CN_Name+" "+ item.EN_Name;
                _projectNames.Add(str);
            }
            ProjectMedicineNames= new ObservableCollection<string>(_projectNames.OrderBy(o=>o.FirstOrDefault()));
        }

        /// <summary>
        /// 窗体全屏大小切换
        /// </summary>
        public void FullScreen()
        {
            if (windowState == WindowState.Maximized)
            {
                windowState = WindowState.Normal;
            }
            else if (windowState == WindowState.Normal)
            {
                windowState = WindowState.Maximized;
            }
        }

        /// <summary>
        /// 窗体最小化
        /// </summary>
        /// <param name="parameter"></param>
        public void MinimizeScreen()
        {
            windowState = WindowState.Minimized;
        }

        private string nowTimeValue;//当前时间
        public string NowTimeValue
        {
            get { return nowTimeValue; }
            set
            {
                nowTimeValue = value;
                this.RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 每秒更新主界面时间
        /// </summary>
        private void RefreshTimeValue()
        {
            var task = Task.Factory.StartNew(new Action(async () =>
            {
                while (true)
                {
                    NowTimeValue = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                    await Task.Delay(1000);
                }
            }));
        }
    }
}
